<?
function DateThai($strDate) {
	$strYear = date("Y", strtotime($strDate)) + 543;
	$strMonth = date("n", strtotime($strDate));
	$strDay = date("j", strtotime($strDate));
	//$strHour= date("H",strtotime($strDate));
	//$strMinute= date("i",strtotime($strDate));
	//$strSeconds= date("s",strtotime($strDate));
	$strMonthCut = Array("", "ม.ค.", "ก.พ.", "มี.ค.", "เม.ย.", "พ.ค.", "มิ.ย.", "ก.ค.", "ส.ค.", "ก.ย.", "ต.ค.", "พ.ย.", "ธ.ค.");
	$strMonthThai = $strMonthCut[$strMonth];
	return "$strDay $strMonthThai $strYear";
}

function toEngDate($date) {
	list($day, $month, $year) = explode("/", $date);
	$year = $year - 543;
	return $year . "-" . $month . '-' . $day;

}

function ansi2UTF($string) {
	return $utf8String = mb_convert_encoding($string, 'UTF-8', 'US-ASCII');
}

function send_mail($to, $subject, $message) {
	require_once 'phpmail/class.phpmailer.php';
	$mail = new PHPMailer();
	$msg = $message;
	$myEmail = "info@senzitiveshop.com";

	$body = $msg;
	$mail -> SetFrom($myEmail, $myEmail);
	$address = $to;
	
	## Lex Added for send mail ##
	$mail -> IsHTML (true);		
	$mail -> IsSMTP();
	$mail -> SMTPAuth = true;
	$mail -> Host = "119.59.120.10";
	$mail -> Port = 25;
	$mail -> Username = $myEmail;
	$mail -> Password = "asdf123#";
	$mail -> Subject = $subject;
	$mail -> CharSet = 'UTF-8';
	########################
	
	//$mail -> IsSMTP();
	// telling the class to use SMTP
	//$mail->Host       = "mail.majortour.co.th"; // SMTP server
	//$mail -> SMTPDebug = 1; //Lex comment out
	//$mail -> SMTPAuth = true;
	// enable SMTP authentication
	//$mail -> SMTPSecure = "tls"; //Lex comment out
	// sets the prefix to the servier
	//$mail -> Host = "119.59.120.10";
	// sets GMAIL as the SMTP server
	//$mail -> Port = 25;
	// set the SMTP port for the GMAIL server
	//$mail -> Username = $myEmail;
	// GMAIL username
	//$mail -> Password = "asdf123#";
	//$mail -> Subject = $subject;
	//$mail -> CharSet = 'UTF-8';


	$emailList = explode(",", $to);
	if (!empty($emailList)) {
		foreach ($emailList as $in) {

			$mail -> AddAddress($in, $in);
		}
	}
	$mail -> AltBody = "To view the message, please use an HTML compatible email viewer!";
	// optional, comment out and test
	$mail -> MsgHTML($body);
	if (!$mail -> Send()) {
		echo "Mailer Error: " . $mail -> ErrorInfo;
		return false;
	} else {
		return true;
	}
}
function converUnit($items,$option){
	
	$baseUnit = $option['BASE_UNIT'];
	$remainQty = $items['qty'];
	$q1 = "";
	$q2 = "";
	$q3= "";
	$c1 = floor($remainQty / $option['EA_CARTON']);
	if($c1 > 0){
		$remainQty  = $remainQty - ($c1 * $option['EA_CARTON']);
		$q1 = $c1." ลัง "; 
	}
	$c2 = floor($remainQty / $option['EA_PACK']);
	if($c2 > 0){
		$remainQty  = $remainQty - ($c2 * $option['EA_PACK']);
		$q2 = $c2." แพ็ก "; 
	}
	if($remainQty > 0 ){
		$q3 = $remainQty." ชิ้น ";
	}
	
	return $q1.$q2.$q3;
}
function converUnitCombo($items,$option){
		
	$baseUnit = $option['BASE_UNIT'];	
	
	
	$remainQty  = $items['qty'] - 1;
	
	$q1 = "";
	$q2 = "";
	$q3= "";
	$first = true;
	$c1 = floor($remainQty / $option['EA_CARTON']);
	
	$start = '<span><input type="checkbox" class="splitCheck" value="'.$items['qty'].'"> แยกส่ง</span>';
	if($c1 > 0){
		
		$remainQty  = $remainQty - ($c1 * $option['EA_CARTON']);
		$html = '<select ratio="'.$option['EA_CARTON'].'">';
		for($i=0;$i <= $c1 ;$i++){
			$html.='<option value="'.$i.'">'.$i.'</option>';
		}
		$html.='</select>';
		$q1 = $html." ลัง "; 
	}
	$c2 = floor($remainQty / $option['EA_PACK']);
	
	if($c2 > 0){
		
		$remainQty  = $remainQty - ($c2 * $option['EA_PACK']);
		//echo $remainQty;exit();
		$html = '<select ratio="'.$option['EA_PACK'].'">';
		for($i=0;$i<= $c2 ;$i++){
			$html.='<option value="'.$i.'">'.$i.'</option>';
		}
		$html.='</select>';
		$q2 = $html." แพ็ก "; 
	}
	if($remainQty > 0 ){
		$html = '<select ratio="1">';
		for($i=0;$i<= $remainQty ;$i++){
			$html.='<option value="'.$i.'">'.$i.'</option>';
		}
		$html.='</select>';
		$q3 = $html." ชิ้น ";
	}
	
	return $start.'<br /><span class="split" style="display:none">'.$q1.$q2.$q3.'<input type="hidden" default-value="'.$items['qty'].'" value="'.$items['qty'].'" name="sendQty[]"> </span>';
}
function converUnitView($items,$option = null){
	
	$items = (array)$items;
	$baseUnit = $items['SaleUnit'];
	$remainQty = $items['Qty'];
	$q1 = "";
	$q2 = "";
	$q3= "";
	$c1 = floor($remainQty / $items['CartonRatio']);
	if($c1 > 0){
		$remainQty  = $remainQty - ($c1 * $items['CartonRatio']);
		$q1 = $c1." ลัง "; 
	}
	$c2 = floor($remainQty / $items['PackRatio']);
	if($c2 > 0){
		$remainQty  = $remainQty - ($c2 * $items['PackRatio']);
		$q2 = $c2." แพ็ก "; 
	}
	if($remainQty > 0 ){
		$q3 = $remainQty." ชิ้น ";
	}
	
	return $q1.$q2.$q3;
	
	
}
function displayPromotionFormat($promotion){
	
	if(!empty($promotion)){
		$text = "";
		$discount= 0;
		foreach($promotion as $in=>$val){
			$text.=$val['QTY']." ".getUnitDesc($in)." ";
			$discount+=$val['DISCOUNT'];
		}
	}
	if($text!=""){
		$text = "ได้รับของแถมทั้งหมด ".$text;
		$text.="ส่วนลด: ".$discount;
	}
	return $text;
}
function getDiscount($promotion){
	
	$discount= 0;
	if(!empty($promotion)){
		$text = "";
		foreach($promotion as $in=>$val){
			$discount+=$val['DISCOUNT'];
		}
	}
	return $discount;
}
function checkUnitAdd($promotion){
	$unitAdd = array();
	if(!empty($promotion)){
		foreach($promotion as $in=>$val){
			if($val['QTY'] > 0 ){
				$unitAdd[$in] = $val;		
			}
		}
	}
	return $unitAdd;
}
function getUnitDesc($key){
	$array['KAR'] = "ลัง";
	$array['PAK'] =  "แพ็ก";
	$array['EA'] = "ชิ้น";
	$array[''] = "-";
	return $array[$key];
}
function converUnitToData($items,$option){
	
	$baseUnit = $option['BASE_UNIT'];
	$remainQty = $items['qty'];
	$q1 = "";
	$q2 = "";
	$q3= "";
	$c1 = floor($remainQty / $option['EA_CARTON']);
	if($c1 > 0){
		$remainQty  = $remainQty - ($c1 * $option['EA_CARTON']);
	}
	$c2 = floor($remainQty / $option['EA_PACK']);
	if($c2 > 0){
		$remainQty  = $remainQty - ($c2 * $option['EA_PACK']);
	}
	
	$array['KAR'] = $c1;
	$array['PAK'] = $c2;
	$array['EA'] = $remainQty;;
	
	return $array;
	
	
}
function converUnitToDataObj($qty,$option){
	
	$baseUnit = $option['BASE_UNIT'];
	$remainQty = $qty;
	$q1 = "";
	$q2 = "";
	$q3= "";
	$c1 = floor($remainQty / $option['EA_CARTON']);
	if($c1 > 0){
		$remainQty  = $remainQty - ($c1 * $option['EA_CARTON']);
	}
	$c2 = floor($remainQty / $option['EA_PACK']);
	if($c2 > 0){
		$remainQty  = $remainQty - ($c2 * $option['EA_PACK']);
	}
	
	$array['KAR'] = $c1;
	$array['PAK'] = $c2;
	$array['EA'] = $remainQty;;
	
	return $array;
	
	
}

function getSaleStatus($key){
	$array['F'] = "Fail";
	$array['C'] =  "Complete";
	$array[''] = "-";
	$array['-'] = "-";
	return $array[$key];
}

function getSaleType($key){
	$array['TAN'] = "รายการขาย";
	$array['TANN'] =  "รายการขาย (แถม)";
	return $array[$key];
}

function htmlContent($data) {
	global $config;

	$html = '<div>';
	$html .= '
<table style="border-collapse:collapse;width:100%;border-top:1px solid #DDDDDD;border-left:1px solid #DDDDDD;">
<thead>
<tr>
<td style="font-size:12px;border-right:1px solid #DDDDDD;border-bottom:1px solid #DDDDDD;background-color:#EFEFEF;font-weight:bold;text-align:left;padding:7px;color:#222222;" colspan="2">Infomation</td>
</tr>
</thead>
<tbody>
<tr>
<td style="font-size:12px;border-right:1px solid #DDDDDD;border-bottom:1px solid #DDDDDD;text-align:left;padding:7px;"><b>firstname :</b> '.$data['firstname'].'<br>
<b>lastname</b> '.$data['lastname'].'<br>
<td style="font-size:12px;border-right:1px solid #DDDDDD;border-bottom:1px solid #DDDDDD;text-align:left;padding:7px;"><b>email:</b> '.$data['email'].'<br>
<b>telephone :</b> '.$data['telephone'].'<br>
</td>
</tr>
</tbody>
</table>
<table style="border-collapse:collapse;width:100%;border-top:1px solid #DDDDDD;border-left:1px solid #DDDDDD;">
<thead>
<tr>
<td style="font-size:12px;border-right:1px solid #DDDDDD;border-bottom:1px solid #DDDDDD;background-color:#EFEFEF;font-weight:bold;text-align:left;padding:7px;color:#222222;">description</td>
</tr>

<tr>
<td style="font-size:12px;border-right:1px solid #DDDDDD;border-bottom:1px solid #DDDDDD;text-align:left;padding:7px;">
'.$data['description'].'<br>
</tr>

</thead>
<tbody>

</tbody>
</table>
</div>';
	return $html;
}
?>
